package com.xiao.controller;

import com.xiao.service.UserService;
import com.xiao.util.JwtUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 创建者：毛
 * 和用户有关的controller 请求前要加/user
 */
@RestController
@RequestMapping("/user")
public class UsersController {

    @Autowired
    private UserService userService;


    //查询用户身份
    @PostMapping("/findidentity")
    public Object FindIdentityController(String token){
        System.out.println(token);
        //根据前台传来的token解密获得uid
        //转成long 会报错
        int uid = (int)JwtUtil.checkJWT(token).get("uid");
        System.out.println(uid);
        String s = userService.FindIdentity(uid);

        if (s != null){
            return s;
        }else return "-999";
    }

    //完善用户信息
    @PostMapping("/perfectinfo")
    public Object PerfectInfo(String token,String nickname,String identity,String gradename,String subjectname){
        System.out.println(token);
        System.out.println(nickname);
        System.out.println(gradename);
        System.out.println(subjectname);

        int uid = (int)JwtUtil.checkJWT(token).get("uid");
        System.out.println(uid);

        if (userService.PerfectUserInfo(uid,nickname,identity,gradename,subjectname)){
            return "1";
        }else {
            return "-999";
        }
    }

    //加入班级
    @PostMapping("/joinclass")
    public Object JoinClass(String token,long classid){

        System.out.println(token);
        System.out.println(classid);

        int uid = (int)JwtUtil.checkJWT(token).get("uid");

        if (userService.JoinClass(uid,classid)){
            return "1";
        }else{
            return "-999";
        }
    }
}
